﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACP.DTO;



namespace ACP.Data
{
    public class QuestionsRepository : Repository<question>
    {
        // Add your own data access methods.
        // This file should not get overridden
        public static List<question> GetAllQuestions()
        {
            List<question> lstQuestions;
            IRepository<question> questionsRepository = new Repository<question>();
            try
            {
                lstQuestions = questionsRepository.GetAll().ToList();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                questionsRepository.Dispose();
            }
            return lstQuestions;

        }

        public static List<ExamInformation> GetRandomQuestions(ref result newExam, int limit, int userId,int examID)
        {
            List<ExamInformation> lstExamInfo = null;
            IRepository<question> questionRepositary = new Repository<question>();
           
            try
            {
                var lstQuestions = questionRepositary.Find(q => q.status == true).Where(etype => etype.examid == examID).OrderBy(c => Guid.NewGuid()).Take(limit);
                StringBuilder sbQuestionIds = new StringBuilder();
                if (lstQuestions != null)
                {
                    int serialNumber = 0;
                    lstExamInfo = new List<ExamInformation>();
                    foreach (question q in lstQuestions)
                    {
                        serialNumber++;
                        ExamInformation examInfo = new ExamInformation()
                        {
                            SerialNumber = serialNumber,
                            QuestionId = q.questionid,
                            ExamId = q.examid,
                            UserId = newExam.uid,
                            QuestionType = q.questiontype,
                            Question = q.question1,
                            Option1 = q.option1,
                            Option2 = q.option2,
                            Option3 = q.option3,
                            Option4 = q.option4,
                            Option5 = q.option5,
                            OriginalAnswer = q.answer,
                            AnswerExplanation = q.description
                        };

                        lstExamInfo.Add(examInfo);
                        //QuestionIds to Result table
                        sbQuestionIds.Append(q.questionid + ",");
                    }

                    newExam.questionsids = sbQuestionIds.ToString().TrimEnd(',');

                    ResultsRepository.InsertCurrentExamInformation(newExam);

                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                string ErrorMessage = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        ErrorMessage += "Property:  " + validationError.PropertyName + ", Error: " + validationError.ErrorMessage;
                    }
                }
                throw new Exception(ErrorMessage);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                questionRepositary.Dispose();
            }
            return lstExamInfo;

        }

        public static question GetQuestionById(int id)
        {
            IRepository<question> questionsRepository = new Repository<question>();
            question questionEntity;
            try
            {
                questionEntity = questionsRepository.First(q => q.questionid == id);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                questionsRepository.Dispose();
            }
            return questionEntity;
        }

        public static void UpdateQuestion(question questionObject)
        {
            Repository<question> questionsRepository = new Repository<question>();
            try
            {
                questionsRepository.Update(questionObject);
                questionsRepository.UnitOfWork.Commit();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                questionsRepository.Dispose();
            }
        }

        public static void InsertQuestion(question questionObject)
        {
            Repository<question> questionsRepository = new Repository<question>();
            try
            {
                questionsRepository.Add(questionObject);
                questionsRepository.UnitOfWork.Commit();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                questionsRepository.Dispose();
            }
        }

        public static List<question> GetDistinctRandomQuestions(int userId)
        {
            List<question> lstQuestions=null;
            Repository<question> questionsRepository = new Repository<question>();
            Repository<tempexam> tempRepository = new Repository<tempexam>();
            try
            {
               // int[] questionIds = tempRepository.AsQueryable().Where(u => u.Id == userId).Select(x => x.QuestionId).ToArray<int>();
               // lstQuestions = questionsRepository.AsQueryable().Except(
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                questionsRepository.Dispose();
            }
            return lstQuestions;

        }
    }
}
